#include "LoadSoundState.h"
#include "Game.h"
#include "TestState.h"
#include "ScreenManager.h"
#include "CameraManager.h"
#include "DX11HelpfulFunctions.h"
#include "Timer.h"
#include "DX11TextureManager.h"

LoadSoundState::LoadSoundState()
{
	m_text.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_blinker.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_shiftDown = false;
	m_filePath = "File Path:";
	m_loadFilePath = "File Path:";
	m_blinkTimer = 0.0f;
	m_textSpacing = TheFontManager::Instance()->GetFontSize("pixel_small");
	m_blinking = false;
	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();
}

void LoadSoundState::Draw()
{
	TheScreenManager::Instance()->GetDeviceContext()->OMSetRenderTargets(1, 
		TheScreenManager::Instance()->GetRenderTargetview(),
		TheScreenManager::Instance()->GetDepthStencilView());		
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);
	TheCameraManager::Instance()->Update();
	
	DX11::ClearColour(0.6039f, 0.5960f, 1.0f, 1.0f);

	TheCameraManager::Instance()->Draw();


	D3DXMATRIX m_view;
	D3DXMATRIX m_proj;

	D3DXMatrixLookAtLH(&m_view, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), 
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	
	D3DXMatrixOrthoLH(&m_proj, m_screenSize.x, m_screenSize.y, 0, 100);

	TheScreenManager::Instance()->SetViewProjMatrix(m_view, m_proj);
			
	TheScreenManager::Instance()->EnableBlending();
	TheObjMeshManager::Instance()->ChangeTexture("title_quad",
		TheDX11TextureManager::Instance()->UseTexture(L"../../Assets/load_image.png"));
	//TheObjMeshManager::Instance()->Translate("title_quad", D3DXVECTOR3(0.0f, -1.0f, rot));
	TheObjMeshManager::Instance()->Scale("title_quad", D3DXVECTOR3(m_screenSize.x, m_screenSize.y, 1.0f));
	//TheObjMeshManager::Instance()->Rotate("title_quad", M_PI_4, 1.0f, 0.0f, 0.0f);
	TheObjMeshManager::Instance()->Draw("title_quad");
	TheScreenManager::Instance()->DisableBlending();


	m_text.Draw();
	m_blinker.Draw();
}

void LoadSoundState::Update()
{
	m_blinkTimer += TheTimer::Instance()->GetDt();
	static std::string m_blinkerText;
	if (m_blinkTimer > 0.4f)
	{
		m_blinkerText = "";
		for (int i = 0; i != m_filePath.size(); i++)
		{
			m_blinkerText.push_back(' ');
		}
		m_blinkerText.push_back(' ');

		m_blinkTimer = 0.0f;
		if (m_blinking)
		{
			m_blinking = false;
			m_blinkerText.push_back('|');
		}
		else
		{
			m_blinking = true;
		}
	}
	
	m_blinker.Update(m_blinkerText);
	m_blinker.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0f));

	m_text.Update(m_filePath);
	m_text.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
}

void LoadSoundState::OnActivated()
{
	TheEventPoller::Instance()->AddHandler(this);
}

void LoadSoundState::OnDeactivated()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void LoadSoundState::OnKeyDownEvent(const WPARAM& event)
{
	char c = MapVirtualKey(event, MAPVK_VK_TO_CHAR);
	
	if (event == VK_SHIFT)
	{
		m_shiftDown = true;
	}

	if (m_shiftDown)
	{
		if (c >= 'A' && c <= 'Z')
		{
			m_filePath.push_back(c);
			m_loadFilePath.push_back(c);
		}
	}
	else
	{
		if (c >= 'A' && c <= 'Z')
		{
			m_filePath.push_back(c + 32);
			m_loadFilePath.push_back(c + 32);
		}
	}
	
	std::cout << c << std::endl;
	int fs = '/';

	if (c == '\\')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c == 47)
	{
		m_filePath.push_back(c - 17);
		m_loadFilePath.push_back(c);
	}
	else if (c == '.')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c == ',')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c == '_')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c == '-')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c == ' ')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	else if (c >= '0' && c <= '9')
	{
		m_filePath.push_back(c);
		m_loadFilePath.push_back(c);
	}
	
	std::string fp = "File Path:";
	if (event == VK_BACK)
	{
		if (m_filePath.size() > fp.size())
		{
			m_filePath = std::string(m_filePath.begin(), m_filePath.end() - 1);
			m_loadFilePath = std::string(m_loadFilePath.begin(), m_loadFilePath.end() - 1);
		}
	}
	else if (event == VK_RETURN)
	{
		TheTestState::Instance()->SetSound(
			std::string(m_loadFilePath.begin() + fp.size(), m_loadFilePath.end()));
		TheGame::Instance()->SetGameState(TheTestState::Instance());
	}
}

void LoadSoundState::OnKeyUpEvent(const WPARAM& event)
{
	if (event == VK_SHIFT)
	{
		m_shiftDown = false;
	}
}